/**
* 版权声明：厦门睿商网络科技有限公司 版权所有 违者必究
* 日    期：2020-04-27
*/
package com.rzico.account.controller.member;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.rzico.account.entity.Deposit;
import com.rzico.account.service.DepositService;
import com.rzico.annotation.Log;
import com.rzico.base.BaseController;
import com.rzico.base.CommResult;
import com.rzico.basics.entity.Enterprise;
import com.rzico.basics.entity.Member;
import com.rzico.basics.service.EnterpriseService;
import com.rzico.basics.service.MemberService;
import com.rzico.core.service.SysSequenceService;
import com.rzico.entity.PageResult;
import com.rzico.entity.Pageable;
import com.rzico.util.StringUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 钱包账单控制层
 *
 * @author Rzico Boot
 * @version 1.0
 * @date 2020-04-27
 */
@Api(description = "钱包账单接口")
@RestController
@RequestMapping("/member/deposit")
public class DepositMemberController extends BaseController {

    @Autowired
    private DepositService depositService;

    @Autowired
    private MemberService memberService;

    @Autowired
    private EnterpriseService enterpriseService;

    @Autowired
    private SysSequenceService sysSequenceService;

    /**
     * 分页查询钱包账单
     *
     * @return
    */
    @ApiOperation("分页查询钱包账单")
    @GetMapping("/list")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "month", value = "月份", dataType = "String", paramType = "query")
    })
    public CommResult<Deposit> list(String month,Pageable pageable) {
        Map<String, Object> params = new HashMap<String, Object>();
        params = buildSortField(params, pageable);
        Member member = memberService.getCurrent();

        params.put("memberId", member.getId());
        if (month!=null) {
            String m = month.concat("-01");
            try {
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                Date d = sdf.parse(m);
                d = DateUtils.addMonths(d, 1);
                d = DateUtils.addMilliseconds(d, -1);
                params.put("endDate", d);
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }

        Page<Object> startPage = PageHelper.startPage(pageable.getPageNum(), pageable.getPageSize());
        List<Deposit> list = depositService.selectList(params);
        PageResult<Deposit> pageResult = new PageResult<Deposit>(list, startPage.getTotal(), pageable);
        return CommResult.success(pageResult);

    }

    /**
     * 计算提现金额
     *
     * @param amount
     * @return
     */
    @ApiOperation("计算订单金额（返回实际到账金额）")
    @PostMapping("/calculate")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "amount", value = "提现金额", dataType = "String", paramType = "query")
    })
    public CommResult<BigDecimal> calculate(BigDecimal amount) {
        Member member = memberService.getCurrent();
        Enterprise enterprise = enterpriseService.selectByPrimaryKey(member.getEnterpriseId());
        return CommResult.success(depositService.calculate(enterprise,amount));
    }

    /**
     * 提现
     *
     * @param amount
     * @return
     */
    @ApiOperation("提现")
    @PostMapping("/transfer")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "amount", value = "提现金额", dataType = "String", paramType = "query")
    })
    public CommResult<Deposit> transfer(BigDecimal amount,String paymentPluginId) {
        Member member = memberService.getCurrent();
        if (amount.compareTo(BigDecimal.ONE)<0) {
            return CommResult.error("不能小于1元");
        }
        if (amount.compareTo(new BigDecimal(5000))>0) {
            return CommResult.error("不能大于5000元");
        }
        if (paymentPluginId!=null) {
            paymentPluginId = "weixinWalletPlugin";
        }
        Deposit deposit = depositService.transfer(member,amount,paymentPluginId,sysSequenceService.generate("transfer"));
        return CommResult.success(deposit);

    }

}
